package org.irenty.storage;

import static org.springframework.data.mongodb.core.query.Criteria.where;

import java.util.Arrays;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.irenty.domain.League;
import org.irenty.domain.Season;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;

import com.mongodb.Mongo;

public class MongoApp {

    private static final Log log = LogFactory.getLog(MongoApp.class);

    public static void main(String[] args) throws Exception {

      MongoOperations mongoOps = new MongoTemplate(new Mongo(), "test-database");

      Season s11 = new Season("Premiere League Season 11/12", 2011, 2012);
      Season s12 = new Season("Premiere League Season 10/11", 2010, 2011);
      mongoOps.insert(Arrays.asList(new Season[] {s11, s12}), Season.class);
      
      League league1 = new League("Premiere League", "England");
      league1.getSeasons().addAll(Arrays.asList(new Season[] {s11, s12}));
      
      mongoOps.insert(league1);
      
      League league2 = new League("Championship", "England");
      
      Season s21 = new Season("Championship Season 11/12", 2011, 2012);
      Season s22 = new Season("Championship Season 10/11", 2010, 2011);
      mongoOps.insert(Arrays.asList(new Season[] {s21, s22}), Season.class);
      league2.getSeasons().addAll(Arrays.asList(new Season[] {s21, s22}));
      
      mongoOps.insert(league2);

      log.info(mongoOps.findOne(new Query(where("name").is("Premiere League")), League.class));

      mongoOps.dropCollection("league");
      mongoOps.dropCollection("season");
    }
  }